package cn.camel.algorithm.graph;

/**
 * @author: Camel
 * @description: 邻接矩阵实现
 * @date: 2025/10/20 12:35
 */
public class GraphMatrix {
    private int[][] adjMatrix;
    private int numVertices;
    public GraphMatrix(int numVertices) {
        this.numVertices = numVertices;
        adjMatrix = new int[numVertices][numVertices];
    }

    // 添加边（无向图）
    public void addEdge(int src, int dest) {
        adjMatrix[src][dest] = 1;
        adjMatrix[dest][src] = 1; // 有向图时注释此行
    }

    // 添加带权边
    public void addEdge(int src, int dest, int weight) {
        adjMatrix[src][dest] = weight;
        adjMatrix[dest][src] = weight; // 无向图需反向添加
    }

    // 打印邻接矩阵
    public void printMatrix() {
        for (int i = 0; i < numVertices; i++) {
            for (int j = 0; j < numVertices; j++) {
                System.out.print(adjMatrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
